#ifndef CHARGE_H
#define CHARGE_H

#include"charge/chargeBase.h"
#include"order.h"
#include"organizationInfo.h"
#include<vector>
using std::vector;

//收费类
class Charge
{
protected:
    Charge(){

    }

public:

    static Charge* getInstance(){
        if(charge==nullptr)
        {
            charge=new Charge();
        }
        return charge;
    }

    //添加收费器
    void addChargeFacility(ChargeBase* cb)
    {
        charges.push_back(cb);
    }

    bool chargePrice(Order order,int maxTime=10000)//设置价格和最大等待时间
    {
        bool status=false;
        for(auto ch : charges)
        {
            ch->setOrder(&order);
            ch->setStatus(&status);
            ch->start();//开始收费
        }
        //直到等待收费成功
        int curTime=0;
        while (!status) {
            _sleep(100);
            curTime+=100;
            if(curTime<maxTime)//超时
                break;
        }
        //所有线程停止
        for(auto ch:charges)
            ch->quit();
        return status;
    }

protected:
    //所有的收费器
    vector<ChargeBase*> charges;

    static Charge* charge;

};

Charge* Charge::charge = nullptr;

#endif // CHARGE_H
